import axios from "axios";
import { notification } from "ant-design-vue";

function request(options) {
  // 返回一个 promise
  return new Promise((resolve, reject) => {
    axios(options)
      .then(res => {
        resolve(res);
      })
      .catch(error => {
        const {
          response: { status, statusText }
        } = error;
        notification.error({
          // 这里使用jsx方式对错误信息进行了样式处理，在这种场景下使用很方便，且h参数必须填写
          // eslint-disable-next-line no-unused-vars
          message: h => (
            <div>
              请求错误 <span style="color: red">{status}</span> : {options.url}
            </div>
          ),
          description: statusText
        });
        reject(error);
      });
  });
}

export default request;
